package com.yijdn.common.validation;

import com.yijdn.common.exception.MyException;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import java.util.Set;

/**
 * 校验工具类
 * @author zww
 * @date 2018-03-25 10:20
 * <p>
 * Email 971762302@qq.com
 * <p>
 * Describe:
 */
public class ValidationUtils {
    private static Validator validator;

    static {
        validator = Validation.buildDefaultValidatorFactory().getValidator();
    }

    /**
     * 校验实体工具类
     * @param entity
     * @param group
     * @param <T>
     */
    public static<T> void validateEntity(T entity, Class... group){
        Set<ConstraintViolation<T>> constraintViolations = validator.validate(entity, group);
        if(!constraintViolations.isEmpty()){
            throw new MyException("校验失败");
        }

    }
}
